package com.byxm.wlgw.service;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.byxm.wlgw.bean.XxBean;
import com.byxm.wlgw.common.DataBaseAccess;
import com.byxm.wlgw.query.XxQuery;

public class XxService {
	XxQuery xxQuery=new XxQuery();
	ResultSet rs=null;
	DataBaseAccess dba=null;
	
	
	/**
	 * 保存菜单项
	 * @throws Exception 
	 */
	public boolean saveXx(XxBean xxBean) throws Exception{
		int flag=0;
		try {
			dba= new DataBaseAccess();
			flag=dba.executeUpdate(xxQuery.saveXx(xxBean));
			dba.commit();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		if(flag>0){
			return true;
		}else{
			return false;
		}
		
	}
	public boolean delete(XxBean xxBean) throws Exception{
		int flag=0;
		try {
			dba= new DataBaseAccess();
			flag=dba.executeUpdate(xxQuery.deletefXx(xxBean));
			dba.commit();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		if(flag>0){
			return true;
		}else{
			return false;
		}
	}
	
	
	
	/**
	 * 菜单list
	 */
	public List<XxBean> findXxlistByXx(XxBean xxBean){
		List<XxBean>list=new ArrayList<XxBean>();
		try {
			dba= new DataBaseAccess();
			rs=dba.executeQuery(xxQuery.findXxQueryByXx(xxBean));
			while(rs.next()){
				XxBean xxBean2=new XxBean();
				xxBean2.setXxid(rs.getInt("xxid"));
				xxBean2.setXxm(rs.getString("xxm"));
				xxBean2.setFxxid(rs.getInt("fxxid"));
				xxBean2.setXxlx(rs.getInt("xxlx"));
				xxBean2.setPx(rs.getInt("px"));
				xxBean2.setXxurl(rs.getString("xxurl"));
				list.add(xxBean2);
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				dba.releaseConnect();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
		
	}
	
	
	/**
	 * 获得菜单项
	 * @throws Exception 
	 */
	public Map<String,List<XxBean>>  findXxList() throws Exception{
		Map<String,List<XxBean>> mapXx=new HashMap<String,List<XxBean>>();
		try {
			dba= new DataBaseAccess();
			rs=dba.executeQuery(xxQuery.findXxQuery());
			List<XxBean> listFxx=new ArrayList<XxBean>();
			List<XxBean> listSxx=new ArrayList<XxBean>();
			while(rs.next()){
				XxBean xxBean=new XxBean();
				xxBean.setXxid(rs.getInt("xxid"));
				xxBean.setXxm(rs.getString("xxm"));
				xxBean.setFxxid(rs.getInt("fxxid"));
				xxBean.setXxlx(rs.getInt("xxlx"));
				xxBean.setPx(rs.getInt("px"));
				xxBean.setXxurl(rs.getString("xxurl"));
				if(xxBean.getFxxid()==0){
					listFxx.add(xxBean);
				}else{
					listSxx.add(xxBean);
				}
			}
			mapXx.put("listFxx", listFxx);
			mapXx.put("listSxx", listSxx);
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		return mapXx;
	}
	
	
	/****私有方法********/
	
}
